package nl.rintcius.groovy

public class ExpandoBuilder extends BuilderSupport {
    
    private static final String NAME_PROP = "name"
    private static final String VALUE_PROP = "value"
    
    public ExpandoBuilder() {
        super()
        setCurrent(new Expando())
    }
	
	protected void setParent(Object parent, Object child) {
		String childName = child.getProperty(NAME_PROP)
		parent.setProperty(childName, child)
	}
	
	protected Object createNode(Object name) {
		createNode(name, null, null)
	}
	
	protected Object createNode(Object name, Object value) {
		createNode(name, null, value)
	}
	
	protected Object createNode(Object name, Map attributes) {
		createNode(name, attributes, null)
	}
	
	protected Object createNode(Object name, Map attributes, Object value) {
		Expando e = new Expando()
		e.setProperty(NAME_PROP, name)
		e.setProperty(VALUE_PROP, value)
		attributes.each() { k, v ->
			e.setProperty(k,v)
		}
		return e
	}
	
	protected Object postNodeCompletion(Object parent, Object node) {
		return parent
	}	
}
